package com.ldc.wandroid.ui.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ldc.common.model.BasePagingModel;
import com.ldc.common.mvvm.BaseMvvmFragment;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.wandroid.adapter.SquareArticleAdapter;
import com.ldc.wandroid.databinding.FragmentSquareBinding;
import com.ldc.wandroid.model.SquareArticleModel;
import com.ldc.wandroid.viewmodel.SquareViewModel;

import org.jetbrains.annotations.NotNull;

/**
 * A simple {@link Fragment} subclass.
 */
public class SquareFragment extends BaseMvvmFragment<FragmentSquareBinding, SquareViewModel> {
    private int page;
    private SquareArticleAdapter squareArticleAdapter;

    public static SquareFragment newInstance() {
        Bundle args = new Bundle();
        SquareFragment fragment = new SquareFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected FragmentSquareBinding initViewBinding(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return FragmentSquareBinding.inflate(inflater, container, false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.getSquareArticleList().removeObserver(this::getSquareArticle);
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showLoadingView();
        viewModel.getSquareArticleList().observeForever(this::getSquareArticle);
        viewModel.getSquareArticle(page);
        initAdapter();
    }

    private void initAdapter() {
        squareArticleAdapter = new SquareArticleAdapter();
        viewBinding.rv.refreshView.setEnableRefresh(false);
        viewBinding.rv.refreshView.setOnLoadMoreListener(refreshLayout -> {
            page += 1;
            viewModel.getSquareArticle(page);
        });
        viewBinding.rv.recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false));
        viewBinding.rv.recyclerView.setHasFixedSize(true);
        viewBinding.rv.recyclerView.setAdapter(squareArticleAdapter);
    }

    @Override
    protected SquareViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, SquareViewModel.class);
    }


    private void getSquareArticle(BasePagingModel<SquareArticleModel> data) {
        viewBinding.stateView.showContentView();
        viewBinding.rv.refreshView.finishLoadMore();
        viewBinding.rv.refreshView.setNoMoreData(page == data.getPageCount());
        if (page == 0) {
            squareArticleAdapter.setNewData(data.getDatas());
        } else {
            squareArticleAdapter.addNewData(data.getDatas());
        }
    }
}
